package com.empire.module.sporttery.controller.admin.footballmatch;

import com.empire.module.sporttery.controller.app.football.vo.AppFootballMatchPageReqVO;
import com.empire.module.sporttery.vo.FootballDayResultVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.sporttery.controller.admin.footballmatch.vo.*;
import com.empire.module.sporttery.dal.dataobject.footballmatch.FootballMatchDO;
import com.empire.module.sporttery.service.footballmatch.FootballMatchService;

@Tag(name = "管理后台 - 竞猜足彩比赛")
@RestController
@RequestMapping("/sporttery/admin/football-match")
@Validated
public class FootballMatchController {

    @Resource
    private FootballMatchService footballMatchService;

    @PostMapping("/create")
    @Operation(summary = "创建竞猜足彩比赛")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match:create')")
    public CommonResult<Integer> createFootballMatch(@Valid @RequestBody FootballMatchSaveReqVO createReqVO) {
        return success(footballMatchService.createFootballMatch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新竞猜足彩比赛")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match:update')")
    public CommonResult<Boolean> updateFootballMatch(@Valid @RequestBody FootballMatchSaveReqVO updateReqVO) {
        footballMatchService.updateFootballMatch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除竞猜足彩比赛")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sporttery:football-match:delete')")
    public CommonResult<Boolean> deleteFootballMatch(@RequestParam("id") Integer id) {
        footballMatchService.deleteFootballMatch(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得竞猜足彩比赛")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match:query')")
    public CommonResult<FootballMatchRespVO> getFootballMatch(@RequestParam("id") Integer id) {
        FootballMatchDO footballMatch = footballMatchService.getFootballMatch(id);
        return success(BeanUtils.toBean(footballMatch, FootballMatchRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得竞猜足彩比赛分页")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match:query')")
    public CommonResult<PageResult<FootballMatchRespVO>> getFootballMatchPage(@Valid FootballMatchPageReqVO pageReqVO) {
        PageResult<FootballMatchDO> pageResult = footballMatchService.getFootballMatchPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FootballMatchRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出竞猜足彩比赛 Excel")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFootballMatchExcel(@Valid FootballMatchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FootballMatchDO> list = footballMatchService.getFootballMatchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "竞猜足彩比赛.xls", "数据", FootballMatchRespVO.class,
                        BeanUtils.toBean(list, FootballMatchRespVO.class));
    }

    /**
     * 按照统计日期获取数据
     *
     * @return @return {@link FootballDayResultVO} 足球日结果视图对象
     */
    @PostMapping("/getDayResultList")
    @Operation(summary = "获得足球竞猜每天的命中统计")
    public CommonResult<List<FootballDayResultVO>> getDayResultList(@RequestBody AppFootballMatchPageReqVO pageReqVO) {
        List<FootballDayResultVO> pageResult = footballMatchService.getDayResults(pageReqVO);
        return success(pageResult);
    }

    /**
     * 筛选重要场次
     */
    @Operation(summary = "筛选重要场次，记录到数据，可以在界面显示")
    @GetMapping("/select")
    public CommonResult<Boolean> footballSelect(@RequestParam("matchId") Integer matchId, @RequestParam("index") Integer index) {
        Boolean flag = footballMatchService.footballMatchSelect(matchId, index);
        return CommonResult.success(flag);
    }

}